package com.dlyk.manager;

import com.dlyk.mapper.ActivityMapper;
import com.dlyk.mapper.ClueMapper;
import com.dlyk.mapper.CustomerMapper;
import com.dlyk.mapper.TranMapper;
import com.dlyk.result.ChartData;
import com.dlyk.result.SummaryData;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Manager 层：通用业务处理层，它有如下特征：
 * 1） 对第三方平台封装的层，预处理返回结果及转化异常信息。
 * 2） 对 Service 层通用能力的下沉，如缓存方案、中间件通用处理。
 * 3） 与 DAO 层交互，对多个 DAO 的组合复用
 *
 * 这个类对应第三条，注入了多个DAO
 */
@Component
public class StatisticManager {

    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private ClueMapper clueMapper;

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private TranMapper tranMapper;

    /**
     * 加载概览统计数据
     */
    public SummaryData getSummaryData() {

        // 查询正在进行的市场活动数量
        int activityOngoingCount = activityMapper.selectAllOngoingActivity().size();

        // 查询市场活动总数
        int activityTotalCount = activityMapper.selectActivityTotalCount();

        // 查询线索总数
        int clueTotalCount = clueMapper.selectClueTotalCount();

        // 查询客户总数
        int customerTotalCount = customerMapper.selectCustomerTotalCount();

        // 查询交易成功的金额
        BigDecimal tranSuccessAmount = tranMapper.selectTranSuccessAmount();

        // 查询交易总额
        BigDecimal tranTotalAmount = tranMapper.selectTranTotalAmount();

        return SummaryData.builder()
                .activityOngoingCount(activityOngoingCount)
                .activityTotalCount(activityTotalCount)
                .clueTotalCount(clueTotalCount)
                .customerTotalCount(customerTotalCount)
                .tranSuccessAmount(tranSuccessAmount)
                .tranTotalAmount(tranTotalAmount)
                .build();
    }

    /**
     * 获取销售漏斗图表数据
     */
    public List<ChartData> getSaleFunnelChartData() {

        // 查询线索总数
        int clueTotalCount = clueMapper.selectClueTotalCount();

        // 查询客户总数
        int customerTotalCount = customerMapper.selectCustomerTotalCount();

        // 查询交易总数（包含未成交的）
        int tranTotalCount = tranMapper.selectTranTotalCount();

        // 查询交易成交数
        int tranSuccessCount = tranMapper.selectTranSuccessCount();

        /**
         * 注意：返回的数据格式需要是这样的
         *
         *  [
         *      { value: 20, name: '成交' },
         *      { value: 60, name: '交易' },
         *      { value: 80, name: '客户' },
         *      { value: 100, name: '线索' }
         *  ]
         */

        List<ChartData> chartDataList = new ArrayList<>();

        chartDataList.add(ChartData.builder().name("线索").value(clueTotalCount).build());
        chartDataList.add(ChartData.builder().name("客户").value(customerTotalCount).build());
        chartDataList.add(ChartData.builder().name("交易").value(tranTotalCount).build());
        chartDataList.add(ChartData.builder().name("成交").value(tranSuccessCount).build());

        return chartDataList;
    }

    /**
     * 获取线索来源统计饼图数据
     */
    public List<ChartData> getClueSourcePieChartData() {

        // 查询各个线索来源的数量
        List<ChartData> chartDataList = clueMapper.selectClueSourceCount();

        return chartDataList;
    }

    public List<ChartData> getActivityBarChartData() {

        // 查询每月市场活动的数量
        List<ChartData> chartDataList = activityMapper.selectActivityCountByMonth();

        return chartDataList;
    }

    public List<ChartData> getTranLineChart() {

        // 查询每月交易金额
        List<ChartData> chartDataList = tranMapper.selectTranMoneyByMonth();

        return chartDataList;
    }
}

